package com.fly.db.dao;

import android.annotation.TargetApi;
import android.os.Build;

import com.fly.db.exception.DBDaoInitException;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with Android Studio.
 * Package_Name：com.fly.db.dao
 * Project_Name：DBExample
 * User：郭鹏飞
 * Date：2016/1/19
 * Email：love518420@foxmail.com
 * Description：
 */
public class DaoFactory {

    private static DaoFactory instance = null;

    private DaoFactory() {}

    public static DaoFactory getInstance() {
        if (instance == null) {
            synchronized (DaoFactory.class) {
                if (instance == null) {
                    instance = new DaoFactory();
                }
            }
        }
        return instance;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    public BaseDao getDao(Class<? extends BaseDao> clazz, String table) throws DBDaoInitException {
        try {
            Constructor<? extends BaseDao> constructor = clazz.getDeclaredConstructor(String.class);
            return constructor.newInstance(table);
        } catch (InstantiationException | IllegalAccessException |
                NoSuchMethodException | InvocationTargetException e) {
            throw new DBDaoInitException();
        }
    }


}
